package com.akina.matchsystem.controller;


import com.akina.matchsystem.common.BaseResponse;
import com.akina.matchsystem.common.ErrorCode;
import com.akina.matchsystem.common.ResultUtils;
import com.akina.matchsystem.exception.BusinessException;
import com.akina.matchsystem.model.domain.User;
import com.akina.matchsystem.model.request.PrivateChatRequest;
import com.akina.matchsystem.model.request.TeamChatRequest;
import com.akina.matchsystem.model.vo.MessageVO;
import com.akina.matchsystem.service.ChatService;
import com.akina.matchsystem.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/chat")
//@CrossOrigin(origins = {"http://localhost:3000", "http://192.168.56.1:3000" , "http://8.138.130.203","http://www.akinafriend.com:3000"},allowCredentials = "true")
@Slf4j
public class ChatController {
    @Resource
    ChatService chatService;
    @Resource
    UserService userService;

//@PostMapping("/teamchat")
//public BaseResponse<List<Message>>
    @PostMapping("/teamChat")
    public BaseResponse<List<MessageVO>>getTeamChat(@RequestBody TeamChatRequest teamChatRequest){
        //校验
        if(teamChatRequest==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        //加载聊天记录
        //chatService.saveMessage(messageVO);
        List<MessageVO> teamChat = chatService.getTeamChat(teamChatRequest);
        return ResultUtils.success(teamChat);
        //return ResultUtils.success();
    }

    @PostMapping("/privateChat")
    public BaseResponse<List<MessageVO>>getChat(@RequestBody PrivateChatRequest privateChatRequest, HttpServletRequest httpRequest){
        //校验
        if(privateChatRequest==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(httpRequest);
        //加载聊天记录
        //chatService.saveMessage(messageVO);
        List<MessageVO> privateChat = chatService.getChat(privateChatRequest,loginUser);
        return ResultUtils.success(privateChat);
        //return ResultUtils.success();
    }

}
